package com.hr.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.common.retrun.R;
import com.hr.system.entity.SysUser;
import com.hr.system.entity.dto.SysUserDto;
import com.hr.system.entity.vo.SysUserVO;
import com.hr.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hr
 * @since 2024-04-19
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
@CrossOrigin
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

//    @ApiOperation("handler")
//    @GetMapping("handler")
//    public R handler() {
//        return R.ok("handler ok!!!");
//    }
//

    @GetMapping("list")
    public R getList(SysUser sysUser, PageInfo<SysUserDto> pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getSize());
        List<SysUserDto> list = sysUserService.getList(sysUser);
        return R.ok(new PageInfo<>(list));
    }
    @GetMapping("getLeaderList")
    public R getLeaderList(SysUserDto sysUserDto) {
        List<SysUser> list = sysUserService.getLeaderList(sysUserDto);
        return R.ok(new PageInfo<>(list));
    }
    @PostMapping("add")
    public R add(@RequestBody SysUserVO sysUserVO) {
        if (sysUserService.add(sysUserVO) > 0)
            return R.ok("ok");
        else return R.error("error");
    }

    @PostMapping("update")
    public R update(@RequestBody SysUserVO sysUserVO) {
        if (sysUserService.update(sysUserVO) > 0)
            return R.ok("ok");
        else return R.error("error");
    }

    @GetMapping("change")
    public R changeState(Integer id, Integer state) {
        if (sysUserService.changeState(id, state) > 0)
            return R.ok("ok");
        else return R.error("error");
    }


    @PostMapping("delete")
    public R delete(@RequestBody Map<String, Integer> map) {
        if (sysUserService.delete(map.get("id")) > 0)
            return R.ok("ok");
        else return R.error("error");
    }


    @PostMapping("delete_multiple")
    public R deleteMultiple(@RequestBody List<Integer> ids) {
        if (sysUserService.deleteMult(ids) > 0)
            return R.ok("ok");
        else return R.error("error");
    }

    @Operation(summary = "导入Excel")
    @PostMapping("import")
    public R importExcel(@RequestPart MultipartFile file) throws IOException {
        return sysUserService.importExcel(file.getInputStream());
    }
    @Operation(summary = "导出Excel")
    @GetMapping("export")
    public void exportExcel(SysUser sysUser,HttpServletResponse response) throws IOException {
        //设置文件下载
        response.setHeader("Content-Disposition","attachment;filename="+"-user.xlsx");
        sysUserService.exportExcel(sysUser,response.getOutputStream());
    }
}
